package think.thread.c02;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ExceptionThread implements Runnable{
	
	public void run(){
		throw new RuntimeException();
	}
	public static void main(String[] args) {
		try {
			ExecutorService exec=Executors.newCachedThreadPool(new HandlerThreadFactory());
			exec.execute(new ExceptionThread());
		} catch (Exception e) {
//			e.printStackTrace();
		}
		
	}
}

class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{

	public void uncaughtException(Thread t, Throwable e) {
		System.out.println("caught "+e);
//		e.printStackTrace();
	}
	
}

class HandlerThreadFactory implements ThreadFactory{

	public Thread newThread(Runnable r) {
		System.out.println(this+" creating new Thread");
		Thread thread=new Thread(r);
		System.out.println("created "+thread);
		thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
		System.out.println("e="+thread.getUncaughtExceptionHandler());
		return thread;
	}
	
}
